package com.paullorenc.rose.desktop.interfaces

import com.fazecast.jSerialComm.SerialPort
import com.paullorenc.rose.desktop.ForwardMessageBus
import com.paullorenc.rose.desktop.IncomingMessageBus
import com.paullorenc.rose.desktop.OutgoingMessageBus
import com.paullorenc.rose.desktop.conversation.ConversationViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlin.coroutines.coroutineContext

object LoraInterface: RoseInterface {
    private val serialRef = SerialInterface
    override suspend fun start() {
        GlobalScope.launch(Dispatchers.IO) {
            launch {
                OutgoingMessageBus.outgoing.collect {
                    serialRef.write(it)
                }
            }
            launch {
                ForwardMessageBus.forward.collect{
                    serialRef.write(it)
                }
            }
        }
    }
    override suspend fun stop() {
        serialRef.close()
    }
}